#!/bin/bash

#修改hosts
cat <<EOF >> /etc/hosts
192.168.10.59 sjl-master01
192.168.10.60 sjl-node01
192.168.10.61 sjl-node02
EOF

#禁用防火墙和swap
ufw disable
swapoff -a 
sed -i 's/.*swap.*/#&/' /etc/fstab

#启用ipvs
apt install ipvsadm ipset sysstat conntrack -y
cat >> /etc/modules-load.d/ipvs.conf <<EOF 
ip_vs
ip_vs_lc
ip_vs_lblc
ip_vs_lblcr
ip_vs_rr
ip_vs_wrr
ip_vs_sh
ip_vs_dh
ip_vs_fo
ip_vs_nq
ip_vs_sed
ip_vs_ftp
ip_vs_sh
ip_tables
ip_set
ipt_set
ipt_rpfilter
ipt_REJECT
ipip
xt_set
br_netfilter
nf_conntrack
overlay
EOF
systemctl restart systemd-modules-load.service


#修改内核
tee /etc/modules-load.d/containerd.conf <<EOF
overlay
br_netfilter
EOF
modprobe overlay
modprobe br_netfilter
cat <<EOF | tee /etc/sysctl.d/k8s.conf
net.bridge.bridge-nf-call-iptables  = 1
net.bridge.bridge-nf-call-ip6tables = 1
net.ipv4.ip_forward                 = 1
EOF
sysctl --system

#修改系统更新源为国内
sed -i 's/cn.archive.ubuntu.com/mirrors.ustc.edu.cn/g' /etc/apt/sources.list

#安装一些必备软件
apt update -y
apt-get -y install apt-transport-https ca-certificates curl software-properties-common gnupg-agent ntpdate

#安装containerd
apt install -y curl gnupg2 
curl -fsSL https://mirrors.aliyun.com/docker-ce/linux/ubuntu/gpg | gpg --dearmour -o /etc/apt/trusted.gpg.d/docker.gpg
add-apt-repository "deb [arch=amd64] https://mirrors.aliyun.com/docker-ce/linux/ubuntu $(lsb_release -cs) stable" -y

apt update -y
apt install -y containerd.io
containerd config default |  tee /etc/containerd/config.toml >/dev/null 2>&1
sed -i 's/SystemdCgroup \= false/SystemdCgroup \= true/g' /etc/containerd/config.toml
sed -i 's/registry.k8s.io/registry.aliyuncs.com\/google_containers/g' /etc/containerd/config.toml
sed -i 's/pause:3.6/pause:3.9/g' /etc/containerd/config.toml
systemctl restart containerd
systemctl enable containerd


#安装K8S
apt-get update 

# sudo mkdir -p /etc/apt/keyrings # for 比较新的ubuntu版本
curl -fsSL https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | \
	gpg --dearmor -o /etc/apt/keyrings/kubernetes-archive-keyring.gpg
 echo "deb [signed-by=/etc/apt/keyrings/kubernetes-archive-keyring.gpg] https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main" | \
	tee /etc/apt/sources.list.d/kubernetes.list

# curl https://mirrors.aliyun.com/kubernetes/apt/doc/apt-key.gpg | sudo apt-key add - 
# sudo cat <<EOF >/etc/apt/sources.list.d/kubernetes.list
# deb https://mirrors.aliyun.com/kubernetes/apt/ kubernetes-xenial main
# EOF

apt-get update
apt-get install -y kubelet=1.26.9-00 kubeadm=1.26.9-00 kubectl=1.26.9-00
apt-mark hold kubelet kubeadm kubectl
##sudo rm /etc/containerd/config.toml
##sudo systemctl restart containerd
kubeadm init --node-name=sjl-master01 --apiserver-advertise-address=192.168.10.59  --pod-network-cidr=10.244.0.0/16 --service-cidr=10.96.0.0/12  --image-repository=registry.aliyuncs.com/google_containers | tee k8s.txt

#服务器环境配置
mkdir -p $HOME/.kube
cp -i /etc/kubernetes/admin.conf $HOME/.kube/config
chown $(id -u):$(id -g) $HOME/.kube/config
source /usr/share/bash-completion/bash_completion
echo 'source <(kubectl completion bash)' >> ~/.bashrc
kubectl completion bash >/etc/bash_completion.d/kubectl
echo 'alias k=kubectl' >> ~/.bashrc
echo 'complete -F __start_kubectl k' >> ~/.bashrc
source ~/.bashrc
echo "### COMMAND TO ADD A WORKER NODE ###"

tail -3 k8s.txt











